package ${model.packageName};

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import javax.annotation.Resource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kun.shop.base.common.GlobalConfigure;

import org.springframework.stereotype.Service;
<#if model.entityName?if_exists!="">import ${model.basePackage}.entity.${model.entityName};</#if>
<#if model.entityName?if_exists!="">import ${model.basePackage}.dao.${model.entityName}Mapper;</#if>
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
public class ${model.serviceName} {
 	
 	<#if model.entityName?if_exists!="">
 	@Resource
	private ${model.entityName}Mapper ${model.objectName}Mapper;
 	</#if>
 	
 	/**
 	 * 分页查询
	 * @param ${model.objectName}
	 * @param pageNo 页码
	 */
 	public PageInfo<${model.entityName}> queryByPage(${model.entityName} ${model.objectName}, Integer pageNo) {
 		PageHelper.startPage(pageNo, GlobalConfigure.DEFAULT_PAGE_SIZE);
 		Map<String,Object> parameter = new HashMap<String, Object>();
 		// TODO 查询参数
 		List<${model.entityName}> list = ${model.objectName}Mapper.findList(parameter);
 		PageInfo<${model.entityName}> page = new PageInfo<${model.entityName}>(list);
 		return page;
 	}
 	
 	/**
 	 * 新增
	 * @param ${model.objectName}
	 */
 	@Transactional(readOnly = false, rollbackFor = Exception.class)
 	public void addOne(${model.entityName} ${model.objectName}) {
 		if (${model.objectName} == null) {
 			throw new RuntimeException("要保存的对象为空,操作失败.");
 		}
 		this.${model.objectName}Mapper.addOne(${model.objectName});
 	}
 	
 	/**
 	 * 修改
	 * @param ${model.objectName}
	 */
 	@Transactional(readOnly = false, rollbackFor = Exception.class)
 	public void updateOne(${model.entityName} ${model.objectName}) {
 		if (${model.objectName} == null) {
 			throw new RuntimeException("要修改的对象为空,操作失败.");
 		}
 		if (${model.objectName}.getId() == null) {
 			throw new RuntimeException("要修改的对象ID为空,操作失败.");
 		}
 		this.${model.objectName}Mapper.updateOne(${model.objectName});
 	}
 	
 	/**
 	 * 根据ID删除
	 * @param id
	 */
 	@Transactional(readOnly = false, rollbackFor = Exception.class)
 	public void delById(Integer id) {
 		if (null == id) {
 			throw new RuntimeException("缺少参数ID,操作失败.");
 		}
 		this.${model.objectName}Mapper.delById(id);
 	}
 	
 	/**
 	 * 根据ID查询
	 * @param id
	 * @return ${model.entityName}
	 */
 	public ${model.entityName} findById(Integer id) {
 		if (null == id) {
	 		throw new RuntimeException("缺少参数ID,操作失败.");
 		}
 		${model.entityName} ${model.objectName} = this.${model.objectName}Mapper.findById(id);
 		return ${model.objectName};
 	}
}
